package com.pignest.hotel.module.user.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.hotel.beans.Member;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.WhereCreator;

public class MemberListbox extends Listbox  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8029312096873445405L;
	private static final Log LOG = LogFactory.getLog(MemberListbox.class);
	//private final String LST_MEMBER = "memberListbox";
	private MemberService memberService;
	private List<String> sex;
	private String name;
	private String mobile;
	private List<String> type;
	private String mail;
	private String cardNo;
	private Paging page;

	public MemberListbox(){
		sex=new ArrayList<String>();
		type=new ArrayList<String>();
		memberService=Services.getMemberService();
	}
   
	public void onCreate() {

		page =  (Paging)getFellow("pagMember");
		page.setTotalSize(memberService.find("from Member").size());
		final int PAGE_SIZE = page.getPageSize();
		System.out.println("PAGE_SIZE:"+PAGE_SIZE);
		Query(0,PAGE_SIZE);
		page.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	Query(start,PAGE_SIZE);
		      	}
		    	});

		
	}
	public void Query(int first,int pageSize){
		this.getItems().clear();
		StringBuffer hql=new StringBuffer();
		WhereCreator where =new WhereCreator();
		hql.append(" from Member b where 1=1 ");
		if(name!=null&&!name.equals("")){
			hql.append(" and b.name like '%"+name+"%'");
		}
		if(mobile!=null&&!mobile.equals("")){
			hql.append(" and b.mobile like '%"+mobile+"%'");
		}
		if(mail!=null&&!mail.equals(""))
			hql.append(" and b.mail like '%"+mail+"%'");
		if(cardNo!=null&&!cardNo.equals(""))
			hql.append(" and b.cardNo like '%"+cardNo+"%'");
		if(!sex.equals("")&&sex!=null&&sex.size()!=0)
			hql.append(" and b.sex in ("+where.getInstr(sex)+")");
		if(!type.equals("")&&type!=null&&type.size()!=0)
			hql.append(" and b.memberLevel.id in ("+where.getInstr(type)+")");
		List l =(List)memberService.find(hql.toString());
		int Total =l.size();
		List<Member> members=memberService.QueryBy(hql.toString(), first, pageSize);
		
		page.setTotalSize(Total);
		for(int i=0;i<members.size();i++){
			Member member = members.get(i);
			Listitem listitem = new Listitem();
			listitem.setValue(member.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(Integer.toString(i+1)));
			listitem.appendChild(new Listcell(member.getName()));
			//listitem.appendChild(new Listcell(member.getType()));
			if(member.getMemberLevel()!=null)
				listitem.appendChild(new Listcell(member.getMemberLevel().getMemberlevel()));
			else
				listitem.appendChild(new Listcell(""));
			listitem.appendChild(new Listcell(member.getSex()));
			listitem.appendChild(new Listcell(member.getMobile()));
			listitem.appendChild(new Listcell(member.getCertificatType()));
			listitem.appendChild(new Listcell(member.getCertificateNo()));
			listitem.appendChild(new Listcell(member.getMail()));
			//listitem.appendChild(new Listcell(member.getRegisterMode()));
			listitem.appendChild(new Listcell(MoneyUtil.toString(member.getBalance())));
			//listitem.appendChild(new Listcell(member.getSendCardWay()));
			//listitem.appendChild(new Listcell(member.getNation()));
			//listitem.appendChild(new Listcell(member.getProvince()));
			listitem.appendChild(new Listcell(member.getCardNo()));
		}
	
	}
	public void clearQuery(){
		this.sex.clear();
		this.name="";
		this.mail="";
		this.cardNo="";
		this.type.clear();
		Query(0,page.getPageSize());

	}
	public void setSex(Listbox sexListbox) {
		Set<Listitem> selected = sexListbox.getSelectedItems();
		sex.clear();
		for (Listitem listitem : selected) {
			sex.add((String)listitem.getValue());
		}
	}
	public void setType(Listbox typeListbox) {
		Set<Listitem> selected = typeListbox.getSelectedItems();
		type.clear();
		for (Listitem listitem : selected) {
			type.add((String)listitem.getValue());
		}
	}
	public void setName(Textbox textbox){

		name=textbox.getValue();

	}
	public void setMobile(Textbox textbox){
		this.mobile=textbox.getValue();
	}
	public void setMail(Textbox textbox){
		
		this.mail=textbox.getValue();
	}
	public void setCardNo(Textbox textbox){
		this.cardNo=textbox.getValue();
	}
	public String test(){
		return "test";
	}
  public List<Member> QueryMemberList;
}
